<?php
    /* functions database */
    function CheckTransaction($transactionId)
    {
        $sql = "SELECT count(id) FROM c_sms_log WHERE `transactionId`='" . mysql_escape_string($transactionId) . "'";        
        $result = mysql_query($sql);    
        $rows = mysql_fetch_assoc($result);        
        return $rows["count(id)"];
    }
    
    function getUser($refCode)
    {
        $sql = "SELECT id, username FROM c_user WHERE `refCode`='" . mysql_escape_string($refCode) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function getUserById($id)
    {
        $sql = "SELECT id, username FROM c_user WHERE `id`='" . mysql_escape_string($id) . "'";
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function getPercent($gameId)
    {
        $sql = "SELECT percent_wapmaster, percent_partner FROM c_game WHERE id=" . $gameId;
        $result = @mysql_query($sql);    
        $rows = @mysql_fetch_assoc($result);        
        return $rows;   
    }
    
    function CheckGameUser($data)
    {
        $sql = "SELECT count(id) FROM c_game_user WHERE `gameId`='" . mysql_escape_string($data["gameId"]) . "' AND `game_user`='" . mysql_escape_string(trim($data["game_user"])) . "'";        
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        return $rows["count(id)"];   
    }
    
    function InsertGameUser($data)
    {
        $sql = "INSERT INTO c_game_user SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `username`='" . mysql_escape_string($data["username"]) . "'
            , `gameId`='" . mysql_escape_string($data["gameId"]) . "'
            , `game_user`='" . mysql_escape_string(trim($data["game_user"])) . "'            
            , `create_date`=" . time() . "            
        ";        
 
        $result = @mysql_query($sql);
        return $result;   
    }
    
    function InsertSmsLog($data)
    {
        if(!isset($data["action"])) $data["action"] = null;
        
        $sql = "INSERT INTO c_sms_log SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `action`='" . mysql_escape_string($data["action"]) . "'
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "
            , `percent_wapmaster`=" . mysql_escape_string($data["percent_wapmaster"]) . "
            , `percent_partner`=" . mysql_escape_string($data["percent_partner"]) . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function InsertSmsLogTest($data)
    {
        if(!isset($data["action"])) $data["action"] = null;
        
        $sql = "INSERT INTO c_sms_log_test SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `action`='" . mysql_escape_string($data["action"]) . "'
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "
            , `percent_wapmaster`=" . mysql_escape_string($data["percent_wapmaster"]) . "
            , `percent_partner`=" . mysql_escape_string($data["percent_partner"]) . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function InsertSmsLogNoPercent($data)
    {
        if(!isset($data["action"])) $data["action"] = null;
        
        $sql = "INSERT INTO c_sms_log SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `action`='" . mysql_escape_string($data["action"]) . "'
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function InsertSmsLogTestNoPercent($data)
    {
        if(!isset($data["action"])) $data["action"] = null;
        
        $sql = "INSERT INTO c_sms_log_test SET 
            `userId`='" . mysql_escape_string($data["userId"]) . "'
            , `partnerId`=" . mysql_escape_string($data["partnerId"]) . "
            , `type`=" . mysql_escape_string($data["type"]) . "
            , `dataId`=" . mysql_escape_string($data["id"]) . "
            , `content`='" . mysql_escape_string($data["content"]) . "'
            , `output`='" . mysql_escape_string($data["output"]) . "'
            , `sender`='" . mysql_escape_string($data["sender"]) . "'
            , `telco`='" . mysql_escape_string($data["telco"]) . "'
            , `serviceCode`=" . mysql_escape_string($data["serviceCode"]) . "
            , `price`=" . mysql_escape_string($data["price"]) . "
            , `status`=" . mysql_escape_string($data["status"]) . "
            , `transactionId`=" . mysql_escape_string($data["transactionId"]) . "
            , `action`='" . mysql_escape_string($data["action"]) . "'
            , `day`=" . date("d") . "
            , `month`=" . date("m") . "
            , `year`=" . date("Y") . "
            , `create_date`=" . time() . "            
        ";        
        
        $result = @mysql_query($sql);
        return $result;    
    }
    
    function getSmsSyntax()
    {
        $sql = "SELECT syntax FROM c_config_sms";
        $result = @mysql_query($sql);
        $rows = array();    
        while($row = @mysql_fetch_assoc($result)){
            $rows[] = $row;    
        }
        
        return $rows;   
    }
    
    function getTypeSyntax($syntax_code)
    {
        $sql = "SELECT type, serviceCode FROM c_config_sms WHERE `syntax`='" . mysql_escape_string($syntax_code) . "'";        
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        return $rows;   
    }
    
    function getServiceCode($syntax_code, $type)
    {
        $sql = "SELECT type, serviceCode FROM c_config_sms WHERE `syntax`='" . mysql_escape_string($syntax_code) . "' AND `type`='" . mysql_escape_string($type) . "' ";        
        $result = @mysql_query($sql);
        $rows = @mysql_fetch_assoc($result);
        return $rows;   
    }
    
    /* functions tinh */
    function cUrl($url)
    {
        $curl_connection = curl_init($url);
                     
        curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
        curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);
                 
        $result = curl_exec($curl_connection);            
        
        curl_close($curl_connection);                                       
        return $result;                
    }
    
    function listServiceCode()
    {
        return array("6022", "6522", "6622", "6722");
    }
    
    function checkTelco($mobile)
    {        
        $first = substr($mobile, 0, 1);
        if($first=="0"){
            $mobile = substr($mobile, 1, strlen($mobile) - 1);
        } else if($first="8"){
            $mobile = substr($mobile, 2, strlen($mobile) - 2);
        }
        
        $mobile = "84" . $mobile;
        
        $patternViettel = "/^84(9[678]|16[2-9])[\d]{7}/";
        $patternMobi = "/^84(9[03]|12[01268])[\d]{7}/";
        $patternVina = "/^84(9[14]|12[34579])[\d]{7}/";
        $patternVnmobile = "/^84(9[2]|18[86])[\d]{7}/";
        $patternBeeline = "/^84(9[9]|19[9])[\d]{7}/";
        
        if(preg_match($patternViettel, $mobile)){
            $telco = "1";    
        } else if(preg_match($patternMobi, $mobile)){
            $telco = "2";
        } else if(preg_match($patternVina, $mobile)){
            $telco = "3";
        } else if(preg_match($patternVnmobile, $mobile)){
            $telco = "4";
        } else if(preg_match($patternBeeline, $mobile)){
            $telco = "5";
        } else {
            $telco = "0";
        }
        
        return $telco;    
    }
    
    function priceServiceCode($serviceCode)
    {
        $pattern_0 = "/^(\d)0(\d)(\d)$/si";
        $pattern_1 = "/^(\d)1(\d)(\d)$/si";
        $pattern_2 = "/^(\d)2(\d)(\d)$/si";
        $pattern_3 = "/^(\d)3(\d)(\d)$/si";
        $pattern_4 = "/^(\d)4(\d)(\d)$/si";
        $pattern_5 = "/^(\d)5(\d)(\d)$/si";
        $pattern_6 = "/^(\d)6(\d)(\d)$/si";        
        $pattern_7 = "/^(\d)7(\d)(\d)$/si";        
        
        $price = 0;
        if(preg_match($pattern_0, $serviceCode)){
            $price = 500;    
        } else if(preg_match($pattern_1, $serviceCode)){
            $price = 1000;    
        } else if(preg_match($pattern_2, $serviceCode)){
            $price = 2000;    
        } else if(preg_match($pattern_3, $serviceCode)){
            $price = 3000;    
        } else if(preg_match($pattern_4, $serviceCode)){
            $price = 4000;    
        } else if(preg_match($pattern_5, $serviceCode)){
            $price = 5000;    
        } else if(preg_match($pattern_6, $serviceCode)){
            $price = 10000;    
        } else if(preg_match($pattern_7, $serviceCode)){
            $price = 15000;    
        } 
        return $price;
    }
    
    function getRealIpAddr()
    {
        if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
        {
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        }
        elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
        {
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        }
        else
        {
            $ip=$_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }  
    
    function IpPublic()
    {
        $ip = array(
            "210.211.97.7"
            , "210.211.97.14"
            , "210.211.97.18"
            , "127.0.0.1"
        );
        return $ip;
    } 
?>
